<?php
/* @var $this AnalysisController */

//当前商家的优惠券
$criteria=new CDbCriteria;
$criteria->compare('zti_status',1);
$criteria->compare('zti_start_time','<'.time());
$criteria->compare('zti_end_time','>'.time());
$criteria->compare('zti_inventory','>0');
$criteria->compare('zm_id',Company::model()->getCmpUserId());
$ticket = Ticket::model()->findAll($criteria);
$ticket = CHtml::listData($ticket,'zti_id','zti_title');
?>
<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'proPromotion-form',
		'enableAjaxValidation'=>false,
		'htmlOptions'=>array('class'=>'create_box meetgive')
	)); ?>
	<ul class=" form_list2 form_list">
			<li class="c_f"><span class="tit">活动名称：</span>
				<div class="cont f_l">
					<?php echo $form->textField($model,'zcr_title',array('class'=>'t_ipt w_250','maxlength'=>10))?>
					<span id="CmpPromotion_zcr_titleTip"></span>
				</div>
			</li>
			<li class="c_f"><span class="tit">活动时间：</span>
				<div class="cont f_l">
					<?php echo $form->textField($model,'zcr_start_time',array('onclick'=>"WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"))?>
					<span class="c_999">-</span>
					<?php echo $form->textField($model,'zcr_end_time',array('onclick'=>"WdatePicker({startDate:'%y-%M-01 00:00:00',dateFmt:'yyyy-MM-dd HH:mm:ss',alwaysUseStartDate:true})"))?>
					<span id="CmpPromotion_zcr_start_timeTip"></span>
				</div>
			</li>
			<li class="c_f title"><h3 class="fs_14">优惠信息</h3></li>
			<li class="c_f"><span class="tit">优惠方式：</span>
				<div class="cont f_l">
					<?php echo $form->radioButtonList($model,'zcr_type',CmpPromotion::model()->getZcrType(),array('template'=>'{input} {label}','class'=>'radio zcr_type_radio click','labelOptions'=>array('class'=>'mr20')))?>
				</div>
			</li>
			<li class="c_f"><span class="tit">生效条件：</span>
				<div class="cont f_l">
					<div class=" select">
						<div class="coupon" id="zcr_condition">
							<?php if(!empty($model->zcr_condition)){ foreach((array)$model->zcr_condition as $val ){?>
							<p><a class="c_06c del_item f_r" href="javascript:" title="">删除</a><span class="fs_14">买家消费满 <strong class="c_f60"><?php echo $val['condition']?></strong> 元
								<?php echo CHtml::hiddenField('CmpPromotion[zcr_condition][condition][]',$val['condition'])?>
								<?php echo CHtml::hiddenField('CmpPromotion[zcr_condition][value][]',$val['value'])?>
								<?php if($model->zcr_type==1){?>
									 减 <strong class="c_f60">10</strong> 元 。
								<?php }elseif($model->zcr_type==2){?>
									赠送优惠券：<strong class="c_f60"><?php echo $ticket[$val['value']]?></strong>
								<?php }?>
							</span></p>
							<?php }}?>
						</div>
					</div>
				</div>
			</li>
			<li class="c_f"><span class="tit">优惠条件：</span>
				<div class="cont f_l"><span class="mr20">买家消费满 <?php echo CHtml::textField('zcr_contion','',array('class'=>'t_ipt w_80','id'=>'zcr_contion'))?>元</span></span></div>
			</li>
			<li class="c_f"><span class="tit">优惠内容：</span>
				<div class="cont f_l">
					<span class=" type_content_1 <?php echo ($model->zcr_type==1)?'':'d_n'?>">减 
						<?php echo CHtml::textField('tricket_content1','',array('class'=>'t_ipt w_80','maxlength'=>8,'id'=>'tricket_content1'))?>元
						<p class="line">
							<?php echo CHtml::link('添加生效条件<b class="mem_bgx left"></b><b class="mem_bgx right"></b>','javascript:',array('class'=>'mem_bgx link_btn mr10','onclick'=>'addItem("1")'))?>
							<span class="c_999">还可添加 <strong class="c_green3">4</strong> 个级层</span>
						</p>
					</span>
					<span class=" type_content_2 <?php echo ($model->zcr_type==2)?'':'d_n'?>">送优惠券
						<?php echo CHtml::dropDownList('tricket_content2','',$ticket,array('class'=>'s_ipt w_120','id'=>'tricket_content2'))?>
						<?php echo CHtml::link(Yii::t('cmp','Create Coupon'),array('createCoupon'),array('class'=>'c_06c'))?>
						<p class="line">
							<?php echo CHtml::link('添加生效条件<b class="mem_bgx left"></b><b class="mem_bgx right"></b>','javascript:',array('class'=>'mem_bgx link_btn mr10','onclick'=>'addItem("2")'))?>
							<span class="c_999">还可添加 <strong class="c_green3">4</strong> 个级层</span>
						</p>
					</span>
					<?php echo $form->error($model,'zcr_condition')?>
				</div>
			</li>
			<li class="c_f"><span class="tit">优惠备注：</span>
				<div class="cont f_l">
					<?php echo $form->textArea($model,'zcr_content',array('class'=>'t_area'))?>
					<?php echo $form->error($model,'zcr_content')?>
				</div>
			</li>
			<li class="c_f save_link"><span class="tit">&nbsp;</span>
				<div class="cont f_l">
					<?php echo CHtml::submitButton(Yii::t('cmp','Save Set'),array('class'=>'mem_btn mem_btn20 mr5'))?>
				</div>
			</li>
		</ul>
<?php $this->endWidget(); ?>

 <script language="javascript" type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/My97DatePicker/WdatePicker.js"></script>
 <script type="text/javascript">
 //选择类型
$('.zcr_type_radio').bind('click',function(){
	var type = $(this).val();
	$('.type_content_1').hide();
	$('.type_content_2').hide();
	$('.type_content_'+type).show();
	$('.del_item').parent().remove();
})
//添加层
function addItem(type){
	var str,acr_value,acr_value_title;
	var condition = $('#zcr_contion').val();
	if(condition.length==0){
		alert("优惠条件不能为空");
		return;	
	}
	
	if(type==1){
		acr_value = $('#tricket_content1').val();
		if(acr_value.length==0){
			alert("优惠内容不能为空");
			return;	
		}
		$('#tricket_content1').val('');
	 	str = '<p><a class="c_06c del_item f_r" href="javascript:" title="">删除</a><span class="fs_14">买家消费满 <input type="hidden" name="CmpPromotion[zcr_condition][condition][]" value="'+condition+'" > <input type="hidden" name="CmpPromotion[zcr_condition][value][]" value="'+acr_value+'" ><strong class="c_f60">'+condition+'</strong> 元 减 <strong class="c_f60">'+acr_value+'</strong> 元 。</span></p>'
	}else if(type==2){
		acr_value = $('#tricket_content2').val();
		if(acr_value.length==0){
			alert("优惠内容不能为空");
			return;	
		}
		acr_value_title =  $('#tricket_content2 option:selected').text();
		str = '<p><a class="c_06c del_item f_r" href="javascript:" title="">删除</a><span class="fs_14">买家消费满<input type="hidden" name="CmpPromotion[zcr_condition][condition][]" value="'+condition+'" > <input type="hidden" name="CmpPromotion[zcr_condition][value][]" value="'+acr_value+'" > <strong class="c_f60">'+condition+'</strong> 元 减 <strong class="c_f60">'+acr_value_title+'</strong> 元 。</span></p>'
	}
	$('#zcr_contion').val('');
	$('#zcr_condition').append(str);
	$('#zcr_contion').removeClass('error');
	$('#tricket_content1').removeClass('error');
}
//删除层
$('.del_item').live('click',function(){
	$(this).parent().remove();
	//alert('d');
});
//提交前的验证，判断有没有填写生效条件
$('#proPromotion-form').bind("submit", function() {
    if($('#zcr_condition p').length<1){
		$('#zcr_contion').addClass('error');
		$('#tricket_content1').addClass('error');
		return false;
    }
    return true;
});
</script>
 
<!-- 表单验证 -->
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidator-4.1.1.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/formValidatorRegex.js"></script>
 <script>
	jQuery(document).ready(function($) {
		$.formValidator.initConfig({theme:"Default",formID:"proPromotion-form",
			onError:function(msg, obj){
						alert(msg);
						return false;
					},
			onsuccess:function(){ return true; }
		});
	})
</script>
<script type="text/javascript" src="<?php echo Yii::app()->baseUrl;?>/js/formvalidata/cmpValidate.js"></script> 